package com.cbd.dao.impl;

import java.math.BigInteger;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.cbd.dao.RenterDao;
import com.cbd.pojo.PageBean;
import com.cbd.pojo.Renter;
import com.cbd.pojo.ShowRenter;
import com.cbd.pojo.User;
import com.cbd.util.IQuantity;
@Repository
public class RenterDaoImpl implements RenterDao{
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public int login(User user) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("select count(*) from Renter r left join r.user u where u.userName=? and u.userPWD=?");
		query.setString(0, user.getUserName());
		query.setString(1, user.getUserPWD());
		Long l = (Long) query.uniqueResult();
		return l.intValue();
	}

	@Override
	public int regist(Renter renter) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		int num = (int) session.save(renter);
		return num;
	}

	@Override
	public Renter findRenterByUserID(int userID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Renter r where r.user.userID = ?");
		query.setParameter(0, userID);
		return (Renter) query.list().get(0);
	}

	@Override
	public int updateRenterByRenter(Renter renter) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		String userPWD = renter.getUser().getUserPWD();
		Query query1 = session.createQuery("update User u set u.userPWD = ? where u.userID = ?");
		query1.setParameter(0, userPWD);
		query1.setParameter(1, renter.getUser().getUserID());
		query1.executeUpdate();
		Query query = session.createQuery("update Renter r set r.renterTel = ?,r.renterAddress = ?,r.renterDescribe = ? where r.user.userID = ?");
		query.setParameter(0, renter.getRenterTel());
		query.setParameter(1, renter.getRenterAddress());
		query.setParameter(2, renter.getRenterDescribe());
		query.setParameter(3, renter.getUser().getUserID());
		return query.executeUpdate();
	}

	@Override
	public List<Object[]> findAllRecordByUserID(int userID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		//投诉过的记录不显示出来 +    AND rif.rentInfoID "
//		+" not in ( "
//		+" 	SELECT ccd.recordID  FROM complaintrecord ccd WHERE ccd.type = false )
		String sql = "SELECT rcd.recordID,pc.carportAddress,pc.carNO,rif.startTime,rif.endTime,us.userName " 
				+" FROM record rcd JOIN purpose pps on rcd.purposeID=pps.purposeID "
				+" JOIN rentinfo rif ON pps.rentInfoID=rif.rentInfoID "
				+" JOIN personalcarport pc ON rif.carportID=pc.carportID "
				+" JOIN landlady ll ON pc.ladyID=ll.ladyID "
				+" JOIN `user` us ON ll.userID = us.userID "
				+" where rif.rentInfoID "
				+" in ( "
						+" SELECT pp.rentInfoID from record rc "
						+"JOIN purpose pp ON rc.purposeID=pp.purposeID "
						+"JOIN renter rr ON pp.renterID = rr.renterID "
						+"JOIN  `user` u ON rr.userID = u.userID "
						+"WHERE u.userID = ?)";
		Query query = session.createSQLQuery(sql);
		query.setParameter(0, userID);
		return query.list();
	}

	@Override
	public List<Object[]> findOnePageRecordByUserID(int userID, int pageSize, int currentPage) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		String sql = "SELECT rcd.recordID,pc.carportAddress,pc.carNO,rif.startTime,rif.endTime,us.userName " 
				+" FROM record rcd JOIN purpose pps on rcd.purposeID=pps.purposeID "
				+" JOIN rentinfo rif ON pps.rentInfoID=rif.rentInfoID "
				+" JOIN personalcarport pc ON rif.carportID=pc.carportID "
				+" JOIN landlady ll ON pc.ladyID=ll.ladyID "
				+" JOIN `user` us ON ll.userID = us.userID "
				+" where rif.rentInfoID  "
				+" in ( "
						+" SELECT pp.rentInfoID from record rc "
						+"JOIN purpose pp ON rc.purposeID=pp.purposeID "
						+"JOIN renter rr ON pp.renterID = rr.renterID "
						+"JOIN  `user` u ON rr.userID = u.userID "
						+"WHERE u.userID = ?)";
		Query query = session.createSQLQuery(sql);
		query.setParameter(0, userID);
		int beginItem = (currentPage-1)*pageSize;
		query.setFirstResult(beginItem);
		query.setMaxResults(pageSize);
		return query.list();
	}


	@Override
	public List<ShowRenter> showRenter(int page) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createSQLQuery("SELECT c.id,r.renterName,l.ladyName,r.renterTime FROM  renter r"+ 
			" LEFT JOIN purpose p ON r.renterID=p.renterID"+
			" LEFT JOIN record re ON p.purposeID=re.purposeID"+
			" LEFT JOIN complaintrecord c ON re.recordID=c.recordID"+
			" LEFT JOIN rentinfo ren on p.rentInfoID=ren.rentInfoID"+
			" LEFT JOIN personalcarport pe ON ren.carportID=pe.carportID"+
			" LEFT JOIN landlady l ON pe.ladyID=l.ladyID"+
			" WHERE c.compaint=? AND c.type=? LIMIT ?,? ");
		query.setParameter(0, 0);
		query.setParameter(1, 0);
		query.setParameter(2, page);
		query.setParameter(3, IQuantity.pageSize);
		List<ShowRenter>list=query.list();
		return list;
	}

	@Override
	public int count() {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createSQLQuery("SELECT count(*) FROM  renter r"+ 
				" LEFT JOIN purpose p ON r.renterID=p.renterID"+
				" LEFT JOIN record re ON p.purposeID=re.purposeID"+
				" LEFT JOIN complaintrecord c ON re.recordID=c.recordID"+
				" LEFT JOIN rentinfo ren on p.rentInfoID=ren.rentInfoID"+
				" LEFT JOIN personalcarport pe ON ren.carportID=pe.carportID"+
				" LEFT JOIN landlady l ON pe.ladyID=l.ladyID"+
				" WHERE c.compaint=? AND c.type=?");
		query.setParameter(0, 0);
		query.setParameter(1, 0);
		BigInteger count1=  (BigInteger) query.list().get(0);
		String coun=""+count1;
		System.out.println("dao"+Integer.parseInt(coun));
		return Integer.parseInt(coun);
		
	}


	@Override
	public Object[] findRecordByRecordID(int recordID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("select rc.recordID,pc.carportAddress,pc.carNO,ri.startTime,ri.endTime,u.userName from Record rc,"
				+ "Purpose pp,RentInfo ri,Personalcarport pc,Landlady ll,User u,Renter r where rc.recordID = ? and ll.user.userID = u.userID and "
				+ "pc.landlady.ladyID = ll.ladyID and ri.personalcarport.carportID = pc.carportID and pp.rentInfo.rentInfoID = "
				+ "ri.rentInfoID and rc.purpose.purposeID = pp.purposeID and pp.isDeal = true and pc.type = true ");
		query.setParameter(0, recordID);
		Object [] objects =  (Object[]) query.list().get(0);
		return objects;
	}

	@Override
	public void updateRpById(int renterID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Renter r = (Renter) session.get(Renter.class, renterID);
		String [] cha = r.getRenterRP().split("/");
		int num = Integer.parseInt(cha[1]);
		int num1 = num+1;
		String rp =cha[0]+"/"+num1;
		r.setRenterRP(rp);
		Query query = session.createQuery("update Renter r set r.renterRP = ? where r.renterID = ?");
		query.setString(0, rp);
		query.setInteger(1, renterID);
		query.executeUpdate();
	}

}